/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dbhandler.DBConnector;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
import java.net.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.regex.*;
import org.apache.commons.lang.WordUtils;

/**
 *
 * @author Tokino
 */
public class AddProduct {

    private String title;
    private String addinfo;
    private String category;
    private String mtype;
    private String synopsis;
    private String price;
    private Hashtable errors;
    private int accounttype;
    private String bt_submit;

    public AddProduct() {
        title = "";
        addinfo = "";
        category = "";
        mtype = "";
        synopsis = "";
        price = "";
       // bt_submit="";
        errors = new Hashtable();
    }

    public boolean checkproduct() throws SQLException, ClassNotFoundException, NoSuchAlgorithmException, UnsupportedEncodingException {
        //System.out.println(bt_submit+ "button me");
        boolean bool = true;
        DBConnector dbConnectorchk = null;
        dbConnectorchk = new DBConnector("localhost", 3306, "selector", "53l3ct0r", "bookaholic");


        addinfo = WordUtils.capitalizeFully(addinfo);
        if (addinfo.equals("") || !(addinfo.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s\\!][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("addinfo", "Please enter a valid info (alphanumeric (,)(.)(-)( )(!) only)");
            errors.put("status", "Failure");
            addinfo = "";
            bool = false;
        }

        //synopsis = WordUtils.capitalizeFully(synopsis);
        if (synopsis.equals("") || !(synopsis.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s\\!][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("synopsis", "Please enter a valid synopsis (alphanumeric (,)(.)(-)( )(!) only)");
            errors.put("status", "Failure");
            synopsis = "";
            bool = false;
        }

        //price = WordUtils.capitalizeFully(price);
        if (price.equals("") || !(price.matches("^\\$?([1-9]{1}[0-9]{0,2}(\\,[0-9]{3})*(\\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|(\\.[0-9]{1,2})?)$"))) {
            errors.put("price", "Please enter a valid price $[value](up to decimal places, comma optional)");
            errors.put("status", "Failure");
            price = "";
            bool = false;
        }

//Paperback Mass-market Hardcover
        if (accounttype == 3) {
            category = "Book";
            if (!(mtype.equals("Paperback") || mtype.equals("Mass-market") || mtype.equals("Hardcover"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        //Fashion Home & Garden Entertainment Cooking Craft
        if (accounttype == 4) {
            category = "Magazine";
            if (!(mtype.equals("Fashion") || mtype.equals("Home & Garden") || mtype.equals("Entertainment") || mtype.equals("Cooking") || mtype.equals("Craft"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        if (accounttype == 5) {
            category = "Audio CD";
            if (!(mtype.equals("CD") || mtype.equals("MP3"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        if (accounttype == 6) {
            category = "Movie";
            if (!(mtype.equals("DVD") || mtype.equals("Blue-ray"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        
                title = WordUtils.capitalizeFully(title);
        if (title.equals("") || !(title.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s\\!][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("title", "Please enter a valid title (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            title = "";
            bool = false;
        } else if (dbConnectorchk.checktitle(title,mtype)) {
            title = "";
            addinfo = "";
            category = "";
            mtype = "";
            synopsis = "";
            price = "";
            errors = new Hashtable();
            errors.put("title", "title with the same type already exists");
            errors.put("status", "Failure");
            bool = false;
        }

        if (bool == true) {
            DBConnector dbConnector = null;
            dbConnector = new DBConnector("localhost", 3306, "adder", "15mAdd3r", "bookaholic");

            //Create new user
            dbConnector.createProduct(title, addinfo, category, mtype, synopsis, price);
            title = "";
            addinfo = "";
            category = "";
            mtype = "";
            synopsis = "";
            price = "";
            errors = new Hashtable();
            errors.put("status", "Success!  Product Added");
        }


        return bool;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public void setErrors(String key, String msg) {
        errors.put(key, msg);
    }

    public int getAccounttype() {
        return accounttype;
    }

    public void setAccounttype(int accounttype) {
        this.accounttype = accounttype;
    }

    public String getAddinfo() {
        return addinfo;
    }

    public void setAddinfo(String addinfo) {
        this.addinfo = addinfo;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getMtype() {
        return mtype;
    }

    public void setMtype(String mtype) {
        this.mtype = mtype;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
